﻿//-----------------------------------------------------------------------
// <copyright file="WebflowElementResultProvider.cs" company="Sławomir Rosiek">
//     Copyright (c) Sławomir Rosiek. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Rosieks.Web.Flow
{
    using System;
    using System.Collections.Generic;
    using Rosieks.Web.Flow.Metadata;

    public class WebflowElementResultProvider
    {
        internal static readonly Dictionary<Type, Func<IWebflowElementResultAdapter>> Adapters;

        private static readonly object Obj;

        /// <summary>
        /// Initializes static members of the <see cref="T:WebflowElementResultProvider"/> class.    
        /// </summary>
        static WebflowElementResultProvider()
        {
            WebflowElementResultProvider.Obj = new object();
            WebflowElementResultProvider.Adapters = new Dictionary<Type, Func<IWebflowElementResultAdapter>>();
            WebflowElementResultProvider.Initialize();
        }

        public static void RegisterAdapter<TElement, TResultAdapter>()
            where TElement : IExecutionObject
            where TResultAdapter : IWebflowElementResultAdapter, new()
        {
            lock (WebflowElementResultProvider.Obj)
            {
                WebflowElementResultProvider.Adapters[typeof(TElement)] = () => new TResultAdapter();
            }
        }

        internal static void Initialize()
        {
            WebflowElementResultProvider.RegisterAdapter<ViewState, ViewStateResultAdapter>();
            WebflowElementResultProvider.RegisterAdapter<Transition, TransitionResultAdapter>();
            WebflowElementResultProvider.RegisterAdapter<NoTransition, TransitionResultAdapter>();
            WebflowElementResultProvider.RegisterAdapter<ActionTransition, TransitionResultAdapter>();
        }
    }
}
